﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eoffice.Models;
using System.Data;
using System.Data.Entity;

namespace Eoffice.Controllers
{
    public class LinhVucController : Controller
    {
        EofficeDb db = new EofficeDb();

        #region[Index]
        public ActionResult Index()
        {

            string page = "1"; //so phan trang hien tai
            var pagesize = 10; //so ban ghi tren 1 trang
            var numOfNews = 0; //tong so ban ghi co duoc truoc khi phan trang
            int curpage = 0; // trang hien tai dung cho phan trang
            if (Request["page"] != null)
            {
                page = Request["page"];
                curpage = Convert.ToInt32(page) - 1;
            }
            var all = db.LINHVUCs.ToList();
            var pages = all.Skip(curpage * pagesize).Take(pagesize).ToList();

            //var pages = data.sp_Brands_Phantrang(page, productize, "", "").ToList();
            var url = Request.Path;
            numOfNews = all.Count;

            if (numOfNews > 0)
            {
                ViewBag.Pager = Phantrang.PhanTrang(pagesize, curpage, numOfNews, url);
            }
            else
            {
                ViewBag.Pager = "";
            }
            return View(pages);
        }
        #endregion

        #region[Create]
        public ActionResult Create()
        {
            return View();
        }
        #endregion

        #region[Create]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection form, LINHVUC linhvuc)
        {
            var malv = form["MALV"];
            var tenlv = ToFirstUpper(form["TENLV"]);

            linhvuc.MALV = malv;
            linhvuc.TENLV = tenlv;

            db.LINHVUCs.Add(linhvuc);
            db.SaveChanges();
            return RedirectToAction("Index");

        }
        #endregion

        #region[Edit]
        public ActionResult Edit(string id)
        {
            LINHVUC linhvuc = db.LINHVUCs.First(l => l.MALV == id);
            if (linhvuc == null)
            {
                return HttpNotFound();
            }
            return View(linhvuc);
        }
        #endregion
        #region[Edit]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(string id, FormCollection form)
        {
            var linhvuc = db.LINHVUCs.First(l=>l.MALV==id);

            var malv = form["MALV"];
            var tenlv = ToFirstUpper(form["TENLV"]);

            linhvuc.MALV = malv;
            linhvuc.TENLV = tenlv;

            db.SaveChanges();
            return RedirectToAction("Index");

        }
        #endregion
        
        #region[Delete]
        public ActionResult Delete(string id)
        {
            var del = (from linhvuc in db.LINHVUCs
                       where linhvuc.MALV == id
                       select linhvuc).Single();

            db.LINHVUCs.Remove(del);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        #endregion
        #region[MultiDelete]
        public ActionResult MultiDelete()
        {
            foreach (string key in Request.Form)
            {
                var checkbox = "";
                if (key.StartsWith("chk"))
                {
                    checkbox = Request.Form["" + key];
                    if (checkbox != "false")
                    {
                        string id = key.Remove(0, 3);
                        var Del = (from emp in db.LINHVUCs 
                                   where emp.MALV == id 
                                   select emp).SingleOrDefault();
                        db.LINHVUCs.Remove(Del);
                        db.SaveChanges();
                    }
                }
            }
            return RedirectToAction("Index");
        }
        #endregion

        #region[Viết hoa chữ cái đầu các từ và loại bỏ các khoảng trắng thừa ]
        public static string ToFirstUpper(string s)
        {
            if (String.IsNullOrEmpty(s))
                return s;

            string result = "";

            //lấy danh sách các từ  

            string[] words = s.Split(' ');

            foreach (string word in words)
            {
                // từ nào là các khoảng trắng thừa thì bỏ  
                if (word.Trim() != "")
                {
                    if (word.Length > 1)
                        result += word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower() + " ";
                    else
                        result += word.ToUpper() + " ";
                }

            }
            return result.Trim();
        }
        #endregion
    }
}
